﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using DAL;
using BUS;

namespace WEBSITE_BANDEAL.Admin
{
    public partial class SanPham_Edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lit_title.Text = "Update sản phẩm";
            if (!IsPostBack)
            {
                LoadData();
            }

        }

        private void LoadData()
        {
            MA_LOAI_SP.DataSource = LOAI_SAN_PHAMBUS.GetAll();
            MA_LOAI_SP.DataTextField = "Tenloaisp";
            MA_LOAI_SP.DataValueField = "Maloai";
            MA_LOAI_SP.DataBind();
            MA_LOAI_SP.Items.Insert(0, new ListItem("----NO PARENT---", "0"));
            TINH_TP.DataSource = TINH_TPBUS.GetAll();
            TINH_TP.DataTextField = "Ten_tp";
            TINH_TP.DataValueField = tinh_tpColumns.Ma_tinh.ToString();
            TINH_TP.DataBind();

            try
            {
                int id = int.Parse(Request.QueryString["ID"]);
                SAN_PHAM sp = SAN_PHAMBUS.GetByID(id);
                if (sp != null)
                {
                    Ma.Value = sp.Masp.ToString();
                    Ten.Value = sp.Tensp.ToString();
                    MA_LOAI_SP.SelectedValue = sp.Maloaisp.ToString();
                    TINH_TP.SelectedValue = sp.Matinh.ToString();

                    SO_LUONG.Value = sp.Soluong.ToString();
                    DIEU_KIEN_AP_DUNG.Text = sp.Dkapdung.ToString();
                    DIEM_NOI_BAT.Text = sp.Diemnoibat.ToString();
                    GIA_GOC.Value = sp.Giagoc.ToString();
                    GIA_BAN.Value = sp.Giaban.ToString();
                    HinhAnh.Text = sp.Hinhdaidien.ToString();
                    ViewHinh.ImageUrl = sp.Hinhdaidien.ToString();
                    TG_BAT_DAU.Text = sp.Tgbatdau.ToString("MM/dd/yyyy");
                    TG_KETTHUC.Text = sp.Tgketthuc.ToString("MM/dd/yyyy");              

                    
                }
            }
            catch
            {
                Response.Redirect("SanPham_List.aspx");
            }
        }

        protected void btn_Update_Click(object sender, EventArgs e)
        {
           // try
           // {
                SAN_PHAM sp = new SAN_PHAM();
                sp.Masp = int.Parse(Ma.Value);
                sp.Tensp = Ten.Value;
                sp.Maloaisp = int.Parse(MA_LOAI_SP.SelectedValue);


                sp.Tgbatdau = DateTime.Parse(TG_BAT_DAU.Text);
                sp.Tgketthuc = DateTime.Parse(TG_KETTHUC.Text);

                sp.Soluong = int.Parse(SO_LUONG.Value);
                sp.Soluongdaban = 0;
                sp.Matinh = int.Parse(TINH_TP.SelectedValue);
                sp.Dkapdung = DIEU_KIEN_AP_DUNG.Text;
                sp.Diemnoibat = DIEM_NOI_BAT.Text;
                sp.Giagoc = int.Parse(GIA_GOC.Value);
                sp.Giaban = int.Parse(GIA_BAN.Value);
                sp.Trangthai = 1;

                sp.Hinhdaidien = HinhAnh.Text;

                SAN_PHAMBUS.update(sp);
                Response.Write("<script type='text/javascript'>alert('Update thành công'); window.opener.location.reload(true); window.close()</script>");
           // }
           // catch
            //{
           //     Response.Redirect("SanPham_List.aspx");
           // }

        }
    }
}